# nap goi thu vien class
library(class)
library(e1071)

# doc du lieu abalone
data(abalone)
nrow <- length(abalone[,1])
ncol <- length(abalone[1,])

# lay ngau nhien
idx <- sample(nrow, replace = FALSE)
trainrow <- round(2*nrow/3)
trainset <- abalone[idx[1:trainrow],]
testset <- abalone[idx[(trainrow+1):nrow],]

# goi naive Bayes
model <- naiveBayes(trainset[,2:(ncol-1)], trainset[,1], laplace=0.01)

# du doan nhan tap kiem tra
pred <- predict(model, testset[,2:(ncol-1)])

# tinh do chinh xac
acc <- sum(pred == testset[,1])/(nrow - trainrow)
print(acc)


